package com.xoa.service.workflow.FlowOpinion.Impl;

import com.xoa.dao.users.UsersMapper;
import com.xoa.dao.workflow.FlowOpinionMapper;
import com.xoa.model.users.Users;
import com.xoa.model.workflow.FlowOpinion;
import com.xoa.model.workflow.FlowOpinionExample;
import com.xoa.service.workflow.FlowOpinion.FlowOpinionService;
import com.xoa.util.CookiesUtil;
import com.xoa.util.ToJson;
import com.xoa.util.common.L;
import com.xoa.util.common.session.SessionUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class FlowOpinionServiceImpl implements FlowOpinionService {
    @Resource
    private FlowOpinionMapper flowOpinionMapper;
    @Resource
    private UsersMapper usersMapper;

    //查询所有
    public ToJson<FlowOpinion> selectAllFlowOpinion() {
        ToJson<FlowOpinion> json = new ToJson<FlowOpinion>(1, "error");
        try {
            List<FlowOpinion> flowOpinions = flowOpinionMapper.selectByExampleWithBLOBs(new FlowOpinionExample());
            for (FlowOpinion flowOpinion : flowOpinions
            ) {
                flowOpinion.setCreateUserName(usersMapper.getUsernameByUserId(flowOpinion.getCreateUser()));
            }
            if (flowOpinions.size() > 0) {
                json.setMsg("ok");
                json.setObject(flowOpinions);
                json.setFlag(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    @Override
    public ToJson<FlowOpinion> selectFlowOpinion(HttpServletRequest request) {
        ToJson<FlowOpinion> json = new ToJson<FlowOpinion>(1, "error");
        try {
            Cookie redisSessionId = CookiesUtil.getCookieByName(request, "redisSessionId");
            Users user = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(), redisSessionId);
            FlowOpinion flowOpinion2=new FlowOpinion();
            flowOpinion2.setCreateUser(user.getUserId());
            flowOpinion2.setOpinionType("0");
            List<FlowOpinion> flowOpinions = flowOpinionMapper.selectAllFlowOpinion(flowOpinion2);
            for (FlowOpinion flowOpinion : flowOpinions) {
                flowOpinion.setCreateUserName(usersMapper.getUsernameByUserId(flowOpinion.getCreateUser()));
            }
            if (flowOpinions.size() > 0) {
                json.setMsg("ok");
                json.setObject(flowOpinions);
                json.setFlag(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    //添加
    public ToJson<FlowOpinion> insertFlowOpinion(FlowOpinion flowOpinion, HttpServletRequest request) {
        ToJson<FlowOpinion> json = new ToJson<FlowOpinion>(1, "error");
        Cookie redisSessionId = CookiesUtil.getCookieByName(request, "redisSessionId");
        Users user = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(), redisSessionId);
        flowOpinion.setCreateUser(user.getUserId());
        int count = flowOpinionMapper.insert(flowOpinion);
        if (count > 0) {
            json.setMsg("ok");
            json.setFlag(0);
        } else {
            json.setFlag(1);
            json.setMsg("error");
        }
        return json;
    }

    //根据id删除
    public ToJson<FlowOpinion> delFlowOpinion(Integer opinionId) {
        ToJson<FlowOpinion> json = new ToJson<FlowOpinion>(1, "error");
        try {
            int count = flowOpinionMapper.deleteByPrimaryKey(opinionId);
            if (count > 0) {
                json.setMsg("ok");
                json.setFlag(0);
            }
        } catch (Exception e) {
            json.setMsg(e.getMessage());
            L.e("FlowOpinion delRelationFunc:" + e);
            e.printStackTrace();
        }
        return json;
    }


    public ToJson<FlowOpinion> delFlowOpinion2(Integer opinionId) {
        ToJson<FlowOpinion> json = new ToJson<FlowOpinion>(1, "error");
        try {
            FlowOpinion flowOpinion = flowOpinionMapper.selectByPrimaryKey(opinionId);
            if ("0".equals(flowOpinion.getOpinionType())){
                json.setMsg("公共意见不能删除");
                json.setFlag(1);
                return json;
            }
            int count = flowOpinionMapper.deleteByPrimaryKey(opinionId);
            if (count > 0) {
                json.setMsg("ok");
                json.setFlag(0);
            }
        } catch (Exception e) {
            json.setMsg(e.getMessage());
            L.e("FlowOpinion delRelationFunc:" + e);
            e.printStackTrace();
        }
        return json;
    }


    //根据id查询单个信息
    public ToJson<FlowOpinion> selFlowOpinionById(Integer opinionId) {
        ToJson<FlowOpinion> json = new ToJson<FlowOpinion>(1, "error");
        try {
            FlowOpinion flowOpinion = flowOpinionMapper.selectByPrimaryKey(opinionId);
            json.setObject(flowOpinion);
            json.setMsg("ok");
            json.setFlag(0);
        } catch (Exception e) {
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return json;
    }

    //根据id更改信息
    public ToJson<FlowOpinion> updateFlowOpinionById(FlowOpinion flowOpinion, HttpServletRequest request) {
        ToJson<FlowOpinion> json = new ToJson<FlowOpinion>(1, "error");
        int count = flowOpinionMapper.updateByPrimaryKeyWithBLOBs(flowOpinion);
        if (count > 0) {
            json.setMsg("ok");
            json.setFlag(0);
        }
        return json;
    }

}
